package com.hiwan.system.service.data;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hiwan.platform.util.DateUtil;
import com.hiwan.system.dao.data.DataDao;
import com.hiwan.system.dao.sequence.SeqDao;
import com.hiwan.system.pojo.DataInfo;
import com.hiwan.system.pojo.DataViewAuth;
import com.hiwan.system.pojo.HiwanFile;
import com.hiwan.system.util.UserCacheUtils;

@Component
@Service
@Transactional
public class DataService {

	@Autowired
	private DataDao   dataDao;
	@Autowired
	private SeqDao seqDao;
	/**
	 * 新增数据
	 * 
	 * @param DataInfo
	 * @return
	 */
	public int insertDataInfo(DataInfo dataInfo) {
		return dataDao.insertDataInfo(dataInfo);
	}

	/**
	 * 修改数据
	 * 
	 * @param DataInfo
	 * @return
	 */
	public int updateDataInfo(DataInfo dataInfo) {
		return dataDao.updateDataInfo(dataInfo);
	}

	/**
	 * 删除数据
	 * 
	 * @param DataId
	 * @return
	 */
	public int deleteDataInfo(String dataId) {
		return dataDao.deleteDataInfo(dataId);
	}
	
	/**
	 * 查询数据
	 * @param busiId
	 * @return
	 */
	public DataInfo selectDataInfoById(String busiId){
		return dataDao.selectDataInfoById(busiId);
	}
	
	public List<DataInfo> selectDataInfoList(DataInfo dataInfo){
		return dataDao.selectDataInfoList(dataInfo);
	}
	
	/**
	 * 获取业务编号
	 * 
	 * @param applyArea
	 * @return
	 */
	public String getBusiId(String applyArea) {
		int seq = seqDao.getAndIncreaseSeq("busiId");
		String busiId = applyArea + "-"
				+ DateUtil.getDateString(new Date(), "yyyyMMdd") + "-"
				+ StringUtils.leftPad(String.valueOf(seq), 5, '0');
		return busiId;
	}
	
	/**
	 * 保存数据申请信息
	 * 
	 * @param dataApplyInfo
	 * @param dataViewAuth
	 * @param session
	 * @throws Exception
	 */
	public int insertDataInfo(DataInfo dataInfo,
			DataViewAuth dataViewAuth, HttpSession session, File applyFile)
			throws Exception {
		String orgId = UserCacheUtils.getOrgId();
		String userId = UserCacheUtils.getUser().getUserId();
		//List<DataViewAuth> list = new ArrayList<DataViewAuth>();
		dataInfo.setApplyArea(orgId);
		dataInfo.setApplyEmp(userId);
		dataInfo.setApplyState("0");
		dataInfo.setPreApplyState("0");
		dataInfo.setLastModifyArea(orgId);// 最后修改机构
		dataInfo.setLastModifyEmp(userId);// 最后修改人
		// 保存数据申请基本信息
		int result=dataDao.insertDataInfo(dataInfo);

		// 上传文件到NFS服务器
		if (dataInfo.getApplyFile() != null
				&& !"".equals(dataInfo.getApplyFile())) {
			HiwanFile hiwanFile = new HiwanFile(new File(dataInfo.getApplyFile()));
			this.writeFile2NFS(session, hiwanFile);
		}
		// if(!writeFile(session, applyFile)){
		// throw new Exception("上传文件错误！");
		// }
		return result;
	}
	
	/**
	 * 更新数据申请信息
	 * 
	 * @param dataApplyInfo
	 * @param session
	 * @throws Exception
	 */
	public int updateDataInfo(DataInfo dataInfo,
			DataViewAuth dataViewAuth, HttpSession session) throws Exception {
		dataInfo.setLastModifyArea(UserCacheUtils.getOrgId());
		dataInfo.setLastModifyEmp((String) session.getAttribute("sessionUserId"));
		if (dataViewAuth != null) {
			dataViewAuth.setBusiId(dataInfo.getBusiId());
		}
		List<DataViewAuth> dataViewAuthList = this.setDataViewAuthInfo(dataViewAuth, session);
		int result=dataDao.updateDataInfo(dataInfo);
		// 更新数据查看授权信息
		this.updateDataViewAuthList(dataViewAuthList, dataInfo, session);
		// 上传文件到NFS服务器
		if (dataInfo.getApplyFile() != null
				&& !"".equals(dataInfo.getApplyFile())) {
			HiwanFile hiwanFile = new HiwanFile(new File(
					dataInfo.getApplyFile()));
			this.writeFile2NFS(session, hiwanFile);
		}
		return result;
	}
	
	/**
	 * 封装授权查看信息对象列表
	 * 
	 * @param dataViewAuth
	 * @param session
	 */
	public List<DataViewAuth> setDataViewAuthInfo(DataViewAuth dataViewAuth,
			HttpSession session) {
		String orgId = UserCacheUtils.getOrgId();
		String userId = UserCacheUtils.getUser().getUserId();
		List<DataViewAuth> list = new ArrayList<DataViewAuth>();
		String[] authArea = null;
		String[] authUser = null;
		String[] authDept = null;
		int length = 0;
		// 获取页面表单值信息
		if (dataViewAuth != null) {
			if (dataViewAuth.getAuthArea() != null) {
				authArea = dataViewAuth.getAuthArea().split(",");
			}
			if (dataViewAuth.getAuthDept() != null) {
				authDept = dataViewAuth.getAuthDept().split(",");
			}
			if (dataViewAuth.getAuthUser() != null) {
				authUser = dataViewAuth.getAuthUser().split(",");
			}
		}
		// 获取表单的条数
		if (authArea != null) {
			length = authArea.length;
		}
		// 拆分单元格的值
		for (int i = 0; i < length; i++) {
			DataViewAuth po = new DataViewAuth();
			po.setAuthArea(authArea[i].trim());// 授权机构
			if (authDept != null && i < authDept.length) {
				po.setAuthDept(authDept[i].trim());// 授权部门
			}
			if (authUser != null && i < authUser.length) {
				po.setAuthUser(authUser[i].trim());// 授权用户
			}
			po.setLastModifyArea(orgId);// 最后修改机构
			po.setLastModifyEmp(userId);// 最后修改人
			po.setBusiId(dataViewAuth.getBusiId());// 业务编号
			list.add(po);
		}
		return list;
	}
	
	
	/**
	 * 处理数据查看授权列表信息 的更新
	 * 
	 * @param dataViewAuthList
	 * @param session
	 */
	public void updateDataViewAuthList(List<DataViewAuth> list,
			DataInfo dataInfo, HttpSession session) {
		// 删除该业务编号对应的所有数据查看授权记录信息
		//this.deleteDataViewAuthList(dataInfo.getBusiId());
		// 插入新的数据查看授权信息
		//this.saveDataViewAuth(list);
	}
	/**
	 * 上传文件到服务器
	 * 
	 * @param session
	 * @param applyFile
	 * @throws Exception
	 */
	public boolean writeFile2NFS(HttpSession session, HiwanFile applyFile)
			throws Exception {
		if (applyFile != null) {
			try {
				//boolean saveFlag = fileService.saveTempFile(UserUtils.getOrgId(), applyFile.getFileName());
				//return saveFlag;
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			}
		}
		return true;
	}

	
}